<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <!--
  
This is released under an MIT License:


Copyright (C) 2011 by Chuck McCallum

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

  -->
  <head>
    <title>
      Ed's Timer
    </title>
    <style type="text/css">
      body {
        font-family: Sans-Serif;
      }
      .click_to_change {
        cursor: pointer;
      }
    </style>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
    <script src="jquery-ui-1.8.13.custom.min.js" type="text/javascript"></script>
    <script type="text/javascript">
      jQuery(function($){
      
        var interval_id;     
        var last;
        var pause = $("#pause");
      
        $(":input:radio").change(on_radio_click);
        
        $("#add_another").click(function(){
          add_another();
          update_hash();
        });
        
        if (location.hash.length > 1) {
          $(decodeURIComponent(location.hash.substring(1)).split(/\n/))
            .each(function(){
              add_another(this.toString());
            })
        }
        
        $("#sortable").sortable({
          update: function(event,ui){ update_hash(); }
        });
        
        pause.click();  // overrides browser form memory.
        pause.change(); // triggers event
        
        function add_another(label) {
          var row = 
            $("#row_template").clone().removeAttr("id").prependTo(".table_body");
          row.find(":radio").change(on_radio_click);
          row.find(".edit").click(on_edit_click);
          if (label != null) {
            row.find(".label").text(label);
          }
          row.show();
        }
        
        function update_hash() {
          location.hash = encodeURIComponent(
            $.makeArray(
              $(".table_body .label:visible").map(function(){
                return $(this).text()
              })
            ).join("\n")
          )
        }
        
        function on_edit_click() {
          var button = $(this).hide();
          var span = button.siblings(".label");
          $("<input/>").val(span.text()).prependTo(
            span.hide().parent()
          ).blur(function(){
            var input = $(this);
            button.show();
            span.text(input.val()).show();
            input.remove();
            update_hash();
            document.title = span.text()
          }).focus().select();
        }
        
        function on_radio_click() {
          document.title = 
            $(this).parents(".row").find(".label").text();
            
          if (pause.is(":checked")) {
            window.clearInterval(interval_id);
            interval_id = null;
          } else if (interval_id == null) {
            interval_id = window.setInterval(interval_callback,1000);
            last = new Date().getTime();
          }
          
        }
        
        function inc_timer(span,inc) {
          // given a span with a mseconds attribute (as a jq object)
          // and an increment in ms,
          // update the mseconds attribute
          // and the display
          var ms = parseInt(span.attr("mseconds"));
          span.attr("mseconds",ms+inc);
          var s = Math.floor(ms/1000)%60;
          var m = Math.floor(ms/1000/60)%60;
          var h = Math.floor(ms/1000/60/60);
          span.html(
            h+":"
            +(m<10?"0":"")+m+":"
            +(s<10?"0":"")+s
          );
        }
        
        function interval_callback() {
          var now = new Date().getTime();
          var inc = now - last;
          last = now;
          
          inc_timer(
            $(":input:radio:checked").parents(".row").find(".time"),
            inc
          );
          
          inc_timer(
            $("span.total"),
            inc
          );
        }
        
      })
    </script>
  </head>
  <body>
    <button id="add_another">add another</button>
    <div class="table_body" id="sortable">
      <div class="row" id="row_template" style="display: none;" label="(blank)">
        <span class="cell">
          <span class="label">(blank)</span>
          <button class="edit">edit</button>
        </span>
        <span class="cell"><input type="radio" name="tasks"/></span>
        <span class="cell"><span mseconds="0" class="time"></span></span>
      </div>
    </div>
    <div class="table_footer">
      <div class="row" >
        <span class="cell label"><i>Pause</i></span>
        <span class="cell"><input type="radio" name="tasks" checked="checked" id="pause"/></span>
        <span class="cell"></span>
      </div>
      <div class="row" >
        <span class="cell"><b>Total</b></span>
        <span class="cell"></span>
        <span class="cell"><span class="total" mseconds="0"></span>
      </div>
    </div>
  </body>
</html>